#include <iostream>
#include <string.h>
#include <SDL.h>
#include <SDL_image.h>
#include "Game.h"
#include "defines.h"
Game::Game()
{
	gameLoad();
	gameInit();
	gameRun();
}
Game::~Game()
{
	gameClean();
}

void Game::gameClean()
{
	for(int i = 0; i < NUMBER_OF_POULTRIES ; i++)
	{
		delete arrObjects[i];
		arrObjects[i] = NULL;
	}
	delete basket;
	
	SDL_FreeSurface(eggsSurface);// Ham Thu Vien 
	SDL_FreeSurface(levelSurface);// Ham Thu Vien 
	SDL_FreeSurface(gameOverSurface);// Ham Thu Vien 
	SDL_FreeSurface(livesSurface);// Ham Thu Vien 
	SDL_FreeSurface(screen);// Ham Thu Vien 
	SDL_Quit();// Ham Thu Vien 
}
void Game::gameLoad()
{

}
void Game::gameInit()
{
	running = true;
	screen = NULL;
	if (SDL_Init( SDL_INIT_EVERYTHING) == -1) running = false;// Ham Thu Vien 
	screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, 32, SDL_SWSURFACE  );// Ham Thu Vien 
	

	MyRectangle rect ;
	rect.sdlRect.w = SCREEN_WIDTH;
	rect.sdlRect.h = 3;
	fence.setRectangle(rect);
	fence.setScreen(screen);
	fence.setPositions(0, 100);

	clearColor = SDL_MapRGB(screen->format, 255, 0, 0);// Ham Thu Vien 
	SDL_FillRect(screen, NULL, clearColor);// Ham Thu Vien 
	

	basket = new Basket(this, "Images/Ro.png", 1.0);
	currentNumberObject = 0;
	frameRate = 0;
	basket->setSpeed(STEP_SPEED_BASKET_MOVING);
	for(int i = 0; i < 322; i++) 
		 KEYS[i] = false;

	eggs = 0;
	level = 1;

	if(TTF_Init() == 0)
	{
		font = TTF_OpenFont( "Fonts/tahoma.ttf", 28 );
		if (font != NULL)
		{
			textColor.b = 0;
			textColor.g = 0;
			textColor.r = 0;
			textColor.unused = 0;
			increaseEggs(0);
			increaseLevels(0);
			descreaseLives(0);
		}
		gameOverSurface = TTF_RenderText_Solid( font, "Game Over" , textColor );// Ham Thu Vien 
	}
	SDL_WM_SetCaption( "TRAN VAN HUNG GL_SAI_TEST HUNG TRUNG GAME", NULL );// Ham Thu Vien 
	speedForAllofEggs = 1;
	currentState = Game::States::kGameUpdate;
}
void Game::gameRun()
{
	
	//Keep track of the frame count
   
	while (running == true)
	{
		thisTime = SDL_GetTicks();// Ham Thu Vien 
		gameInput();
		gameUpdate();
		gameDraw();
		

		deltaTime = (double)(thisTime - lastTime) / 1000;
		lastTime = thisTime;

		//std::cout << thisTime << " " <<  deltaTime << " " << lastTime << "\n";
	}
}
void Game::gameInput()
{
	if (SDL_PollEvent(&occur)){// Ham Thu Vien 
		if (occur.type == SDL_QUIT)/*Kieu Du Lieu va hang so cua thu vien SDL*/
		{
			running = false;
		}
		if (currentNumberObject < NUMBER_OF_POULTRIES && occur.type == SDL_KEYUP)/*Kieu Du Lieu va hang so cua thu vien SDL*/
		{
			switch (occur.key.keysym.sym)/*Kieu Du Lieu cua thu vien SDL*/
			{
				case SDLK_g:
				{
					Chicken *ck = new Chicken(this, "Images/ga.png", 0.6);
					arrObjects[currentNumberObject] = ck;
					currentNumberObject++;
					ck = NULL;
					break;
				}
				case SDLK_v:
				{
					Duck *du = new Duck(this, "Images/vit.png", 0.6);
					arrObjects[currentNumberObject] = du;
					currentNumberObject++;
					du = NULL;
					break;
				}
				case SDLK_n:
				{
					Goose *go = new Goose(this, "Images/ngan.png", 0.6);
					arrObjects[currentNumberObject] = go;
					currentNumberObject++;
					go = NULL;
					break;
				}
				
			}
		}
		switch (occur.key.keysym.sym)/*Kieu Du Lieu cua thu vien SDL*/
		{
		case SDLK_LEFT:/*Hang so thu vien SDL */
			KEYS[occur.key.keysym.sym] = (occur.type == SDL_KEYDOWN)/*Kieu Du Lieu va hang so cua thu vien SDL*/;
			break;
		case SDLK_RIGHT:/*Hang so thu vien SDL */
			KEYS[occur.key.keysym.sym] = (occur.type == SDL_KEYDOWN)/*Kieu Du Lieu va hang so cua thu vien SDL*/;
			break;
		default:
			break;
		}
	}
	if (currentState == States::kGameUpdate)
		basket->handleInput();
	
}
void Game::gameUpdate()
{
	switch (currentState)
	{
		case States::kGameUpdate:
			basket->update();
			for(int i = 0 ; i < currentNumberObject ; i++)
			arrObjects[i] ->update();
		default:
			break;
	}
	
}
void Game::gameDraw()
{
	gameClearScreen();
	if (eggsSurface != NULL)
		SDL_BlitSurface( eggsSurface, NULL, screen, NULL );// Ham Thu Vien 
	if (levelSurface != NULL)
	{
		SDL_Rect rc ;/*Kieu Du Lieu cua thu vien SDL*/
		rc.w = levelSurface->w;
		rc.h = levelSurface->h;
		rc.x = SCREEN_WIDTH - rc.w;
		rc.y = 0;
		SDL_BlitSurface( levelSurface, NULL, screen, &rc );// Ham Thu Vien 
	}
	if (livesSurface != NULL)
	{
		SDL_Rect rc ;/*Kieu Du Lieu cua thu vien SDL*/
		rc.w = livesSurface->w;
		rc.h = livesSurface->h;
		rc.x = (SCREEN_WIDTH - rc.w ) / 2;
		rc.y = 0;
		SDL_BlitSurface( livesSurface, NULL, screen, &rc );// Ham Thu Vien 
	}
	fence.draw();
	for(int i = 0 ; i < currentNumberObject ; i++)
		arrObjects[i] ->draw();
	basket ->draw();
	if (currentState == States::kGameOver)
	{
		SDL_Rect rc;/*Kieu Du Lieu cua thu vien SDL*/
		rc.x = ( SCREEN_WIDTH - gameOverSurface ->w) / 2;
		rc.y = ( SCREEN_HEIGHT - gameOverSurface ->h) / 2;
		rc.w = gameOverSurface ->w;
		rc.h = gameOverSurface ->h;
		SDL_BlitSurface( gameOverSurface , NULL, screen, &rc );// Ham Thu Vien 
	}
	SDL_Flip(screen);// Ham Thu Vien 
}
void Game::gameClearScreen()
{
	SDL_FillRect(screen, NULL, clearColor);// Ham Thu Vien 
}

void Game::increaseEggs(int eggs)
{
	this->eggs += eggs;
	char charPtr[100] ;
	char charPtr2[100] = "Eggs : ";
	itoa(this->eggs, charPtr,10);
	strcat(charPtr2, charPtr);
	eggsSurface = TTF_RenderText_Solid( font, charPtr2 , textColor );// Ham Thu Vien 
}


void Game::increaseLevels(int level)
{
	this->level += level;
	char charPtr[100] ;
	char charPtr2[100] = "Level : ";
	itoa(this->level, charPtr,10);
	strcat(charPtr2, charPtr);
	levelSurface = TTF_RenderText_Solid( font, charPtr2 , textColor );// Ham Thu Vien 
}

void Game::descreaseLives(int life)
{
	this->basket ->life -= life;
	char charPtr[100] ;
	char charPtr2[100] = "Lives : ";
	itoa(this->basket ->life, charPtr,10);
	strcat(charPtr2, charPtr);
	livesSurface = TTF_RenderText_Solid( font, charPtr2 , textColor );// Ham Thu Vien 
}

void Game::increaseEggsSpeed()
{
	speedForAllofEggs += STEP_SPEED_FALLING_DOWN;

	for(int i = 0 ; i < currentNumberObject ; i++)
	{
		arrObjects[i] ->updateSpeedForAllofEggs();
		arrObjects[i] ->updateSpeedForAllofShits();
	}
}
